package com.egao.orderB.service.impl;

import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.DriverOilMainCardBiz;
import com.egao.orderB.biz.DriverOilRechargeBiz;
import com.egao.orderB.entity.DriverOilMainCard;
import com.egao.orderB.entity.DriverOilRecharge;
import com.egao.orderB.service.DriverOilMainCardService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import com.egao.orderB.mapper.DriverOilMainCardMapper;
//import javax.transaction.Transactional;

/**
 * 油卡服务实现类
 * @author : Doman
 * @date : 2021-07-07 17:00:42
 * @version ：1.0
 */
@Service
public class DriverOilMainCardServiceImpl /*extends ServiceImpl<DriverOilMainCardMapper, DriverOilMainCard> */implements DriverOilMainCardService {

	@Autowired
	private DriverOilMainCardBiz driverOilMainCardBiz;
    @Autowired
    private DriverOilRechargeBiz driverOilRechargeBiz;

    /**
    @Override
    public PageResult<DriverOilMainCard> listPage(PageParam<DriverOilMainCard> page) {
        List<DriverOilMainCard> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<DriverOilMainCard> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JsonResult list(PageResult2<T> page, DriverOilMainCard driverOilMainCard, User loginUser)  throws Exception{
        return driverOilMainCardBiz.list(page,driverOilMainCard,loginUser);
    }

    @Override
    public JsonResult list(DriverOilMainCard driverOilMainCard, User loginUser) {
        return JsonResult.ok().setData(driverOilMainCardBiz.list(driverOilMainCard,loginUser));
    }

    @Override
    public JsonResult getById(DriverOilMainCard driverOilMainCard) {
        return JsonResult.ok().setData(driverOilMainCardBiz.getById(driverOilMainCard));
    }

    @Transactional
    @Override
    public JsonResult save(DriverOilMainCard driverOilMainCard, User loginUser) {
        if (driverOilMainCardBiz.save(driverOilMainCard, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult recharge(DriverOilMainCard driverOilMainCard, User loginUser) {
        DriverOilMainCard driverOilMainCard2 = driverOilMainCardBiz.getById(driverOilMainCard);
        if (driverOilMainCardBiz.recharge(driverOilMainCard, loginUser)) {
            DriverOilRecharge driverOilRecharge = new DriverOilRecharge();
            driverOilRecharge.setCompanyName(driverOilMainCard2.getCompanyName());
            driverOilRecharge.setOilCard(driverOilMainCard2.getCardNo());
            driverOilRecharge.setOil(driverOilMainCard.getOil());
            driverOilRecharge.setPrice(driverOilMainCard.getPrice());
            driverOilRecharge.setReDate(driverOilMainCard.getReDate());
            driverOilRecharge.setType("充值");
            driverOilRecharge.setRemark("主卡充值");
            driverOilRechargeBiz.save(driverOilRecharge, loginUser);
            return JsonResult.ok("充值成功");
        }
        return JsonResult.error("充值失败");
    }

    @Override
    public JsonResult distribution(DriverOilMainCard driverOilMainCard, User loginUser) {
        DriverOilMainCard driverOilMainCard2 = driverOilMainCardBiz.getById(driverOilMainCard);
        if (driverOilMainCardBiz.distribution(driverOilMainCard, loginUser)) {
            DriverOilRecharge driverOilRecharge = new DriverOilRecharge();
            driverOilRecharge.setCompanyName(driverOilMainCard2.getCompanyName());
            driverOilRecharge.setOilCard(driverOilMainCard2.getCardNo());
            driverOilRecharge.setOil(driverOilMainCard.getOil());
            driverOilRecharge.setPrice(driverOilMainCard.getPrice());
            driverOilRecharge.setReDate(driverOilMainCard.getReDate());
            driverOilRecharge.setType("扣款");
            driverOilRecharge.setRemark("主卡扣款");
            driverOilRechargeBiz.save(driverOilRecharge, loginUser);
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Override
    public JsonResult update(DriverOilMainCard driverOilMainCard, User loginUser) {
        if (driverOilMainCardBiz.update(driverOilMainCard, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(DriverOilMainCard driverOilMainCard, User loginUser) {
        if (driverOilMainCardBiz.remove(driverOilMainCard, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
